import mitmproxy.http
from mitmproxy import ctx
import requests

def my_request(path: str, data: bytes) -> str:
    header = {
        'Content-type': 'application/json'
    }
    rsp = requests.post("http://localhost:25020" + path[22:], data=data, headers=header, timeout=2.50)
    return rsp.text

class Interceptor:
    def __init__(self):
        ctx.log.info("init")

    def request(self, flow: mitmproxy.http.HTTPFlow):
        request = flow.request
        if request.url.startswith('https://oa-fat02.wsecar.com/api/oaBusinessManager/app/version/manage') :
            resp_data = my_request(request.path, request.data.content)
            flow.response = mitmproxy.http.Response.make(
                    status_code=200,
                    content=resp_data,
                    headers={"Content-Type": "application/json;charset=utf-8"}
                )

addons = [
    Interceptor()
]